package com.wxtt.android.fragment;

import android.view.View;
import android.widget.AdapterView;

import com.fast.dev.frame.Adapter.listview.BaseListAdapter;
import com.fast.dev.frame.http.callback.JsonHttpCallBack;
import com.fast.dev.frame.utils.AndroidInfoUtils;
import com.fast.dev.frame.utils.StringUtils;
import com.three.WxUtils;
import com.wxtt.android.R;
import com.wxtt.android.adapter.JokeAdapter;
import com.wxtt.android.bean.JokeBean;
import com.wxtt.android.config.ApiConfig;
import com.wxtt.android.dialog.ShareDialog;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 说明：笑话
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2016/2/22 20:49
 * <p/>
 * 版本：verson 1.0
 */
public class JokeFragment extends BaseListFragment<JokeBean> implements ShareDialog.ShareListener {

    private ShareDialog mShareDialog;
    private JokeAdapter mAdapter;
    private List<JokeBean> mDatas = new ArrayList<>();
    //当前请求的页码
    private int currentPage = 0;
    //每页请求的条数
    private int pageNumber = 20;
    private JokeBean shareJokeBean;

    @Override
    public BaseListAdapter<JokeBean> setAdapter() {
        mAdapter = new JokeAdapter(lv,mDatas, R.layout.item_joke);
        return mAdapter;
    }

    private JsonHttpCallBack callBack = new JsonHttpCallBack(){
        @Override
        public void onSuccess(JSONObject result) {
            if (StringUtils.isEquals(result.optString("error_code"),"0")){
                List<JokeBean> data = new JokeBean().toList(result.optJSONObject("result").optString("data"));
                mDatas.addAll(0, data);
            }
            mAdapter.refresh(mDatas);
        }

        @Override
        public void onFailure(int errorCode, String msg) {
            currentPage--;
            if (mDatas.isEmpty()){
                empty_bridge.setFailState(getString(R.string.loading_fail),erroListener);
            }else {
                mActivity.shortMessage(lv, msg);
            }
        }

        @Override
        public void onFinish() {
            super.onFinish();
            swipe_refresh.setRefreshLoadedState();
            if (!mDatas.isEmpty()){
                empty_bridge.setSuccessState();
            }
        }
    };

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position < 0 || position > mDatas.size()){
            return;
        }
        shareJokeBean = mDatas.get(position);
        mShareDialog.show();
    }

    @Override
    public void onFirstUserVisible() {
        super.onFirstUserVisible();
        if (mShareDialog == null){
            mShareDialog = new ShareDialog(mActivity,this);
            mShareDialog.setCanceledOnTouchOutside(true);
            mShareDialog.setCancelable(true);
        }
        empty_bridge.setMyView(swipe_refresh);
        initFirst();
    }

    private View.OnClickListener erroListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            initFirst();
        }
    };

    private void initFirst(){
        empty_bridge.setLoadingState();
        if (AndroidInfoUtils.isNetConnected(mActivity)){
            onRefresh();
        }else {
            empty_bridge.setNetWorkError(getString(R.string.not_network),erroListener);
        }
    }

    @Override
    public void onRefresh() {
        if (currentPage < 0) currentPage = 0;
        currentPage++;
        ApiConfig.queryJoke(currentPage, pageNumber, callBack);
    }

    /**
     * 说明：分享到微信
     */
    @Override
    public void shareWechat() {
        if (shareJokeBean != null){
            String msg = WxUtils.shareText(mActivity,shareJokeBean.getContent(),WxUtils.SCENE_SESSION);
            if (!StringUtils.isEmpty(msg)){
                mActivity.shortToast(msg);
            }
        }
        mShareDialog.dismiss();
    }

    /**
     * 说明：分享到朋友圈
     */
    @Override
    public void shareWxcircle() {
        if (shareJokeBean != null){
            String msg = WxUtils.shareText(mActivity,shareJokeBean.getContent(),WxUtils.SCENE_TIMELINE);
            if (!StringUtils.isEmpty(msg)){
                mActivity.shortToast(msg);
            }
        }
        mShareDialog.dismiss();
    }
}
